package org.example.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.sys.entity.SysAccount;
import org.example.sys.entity.SysAchievement;
import org.example.sys.entity.SysGame;
import org.example.sys.entity.SysPlay;
import org.example.sys.service.ISysAccountService;
import org.example.sys.service.ISysAchievementService;
import org.example.sys.service.ISysGameService;
import org.example.sys.service.ISysPlayService;
import org.example.utlis.BaseController;
import org.example.utlis.ResponseUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 业绩表 前端控制器
 * </p>
 *
 * @author Joey
 * @since 2021-02-27
 */
@RestController
@RequestMapping("/sys/achievement")
@Slf4j
public class SysAchievementController extends BaseController<SysAchievement> {
    @Resource
    ISysAchievementService sysAchievementService;
    @Resource
    ISysAccountService sysAccountService;
    @Resource
    ISysPlayService sysPlayService;

    @ApiOperation(value = "查询业绩列表")
    @GetMapping("/listData")
    public ResponseUtil listData(HttpServletRequest request){
        try {
            List<SysAchievement> list = sysAchievementService.list();
            for(SysAchievement item:list){
                SysAccount account = sysAccountService.getById(item.getAccountId());
                QueryWrapper qw  = new QueryWrapper();
                qw.eq("accountId",item.getAccountId());
                List<SysPlay> playList = sysPlayService.list(qw);
                Integer count  = 0;
                for(SysPlay play:playList){
                    count+=play.getInvestSum();
                }
                item.setNickName(account.getNickName());
                item.setInvestSum(count);
            }


            return ResponseUtil.success(list);
        }catch (Exception e){
            log.error("查询游戏列表出错",e);
            return ResponseUtil.error();
        }
    }

    @Override
    public IService<SysAchievement> getService() {
        return sysAchievementService;
    }
}

